package com.xhwl.logistics.open;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xhwl.logistics.bo.cent.ProjectBO;
import com.xhwl.logistics.dto.push.CarInHuaWeiIotDTO;
import com.xhwl.logistics.dto.push.CarOutHuaWeiIotDTO;
import com.xhwl.logistics.service.business.VehicleDataPushBusinessService;
import com.xhwl.logistics.service.cent.QueryDeviceService;
import com.xhwl.logistics.service.cent.QueryProjectService;
import com.xhwl.logistics.service.impl.redis.VehicleLogRedisServiceImpl;
import com.xhwl.logistics.util.AboutDateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
//@RestController
public class HuaWeiIOTDataPushController {

    @Autowired
    private VehicleLogRedisServiceImpl vehicleLogRedisService;

    @Autowired
    private QueryDeviceService queryDeviceService;

    @Autowired
    private QueryProjectService queryProjectService;

    @Autowired
    private VehicleDataPushBusinessService vehicleDataPushBusinessService;

    private <T> List<T> parse(String pushDataString, Class<T> clazz) {
        JSONObject jsonObject = JSON.parseObject(pushDataString);
        JSONArray jsonArray = jsonObject.getJSONObject("notify_data").getJSONObject("body").getJSONArray("services");
        return jsonArray.stream().map(o -> ((JSONObject)o).getObject("properties", clazz)).collect(Collectors.toList());
    }

    @PostMapping("hua-wei-iot/car-in")
    public void carIn(@RequestBody String carInString) {
        log.info("start: jin-mao push data for car in, data is {}", carInString);
        vehicleLogRedisService.log("access-record-in:" + AboutDateUtil.dateToString(new Date(), "yyyy-MM-dd'T'HHmmss.SSS"), carInString);

        List<CarInHuaWeiIotDTO> carInHuaWeiIotDTOList = parse(carInString, CarInHuaWeiIotDTO.class);
        if (carInHuaWeiIotDTOList.isEmpty()) {
            return;
        }
        String parkId = carInHuaWeiIotDTOList.get(0).getCarinParkId();

        Integer projectId = queryDeviceService.queryProjectIdFromIntegratedConfiguration(parkId);
        ProjectBO projectBO = queryProjectService.queryProject(projectId);

        vehicleDataPushBusinessService.huaWeiIotCarIn(carInHuaWeiIotDTOList, projectBO);

        log.info("end: jin-mao push data for car in");
    }

    @PostMapping("hua-wei-iot/car-out")
    public void carOut(@RequestBody String carOutString) {
        log.info("start: jin-mao push data for car out, data is {}", carOutString);
        vehicleLogRedisService.log("access-record-out:" + AboutDateUtil.dateToString(new Date(), "yyyy-MM-dd'T'HHmmss.SSS"), carOutString);

        List<CarOutHuaWeiIotDTO> carOutHuaWeiIotDTOList = parse(carOutString, CarOutHuaWeiIotDTO.class);
        if (carOutHuaWeiIotDTOList.isEmpty()) {
            return;
        }
        String parkId = carOutHuaWeiIotDTOList.get(0).getCaroutParkId();

        Integer projectId = queryDeviceService.queryProjectIdFromIntegratedConfiguration(parkId);
        ProjectBO projectBO = queryProjectService.queryProject(projectId);

        vehicleDataPushBusinessService.huaWeiIotCarOut(carOutHuaWeiIotDTOList, projectBO);

        log.info("end: jin-mao push data for car out");
    }

}
